package stream;

import pojo.Student;

import javax.lang.model.SourceVersion;
import java.sql.SQLOutput;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class GroupByStream {
    public static void main(String[] args) {
        Stream<Student> stream = Stream.of(
                new Student("101", "猪八戒", 80),
                new Student("102", "孙悟空", 80),
                new Student("103", "撒僧", 80),
                new Student("104", "小白龙", 100),
                new Student("105", "唐僧", 100)
        );
        //按照年龄进行分组
        //Key 年龄  value 对象
//        Map<String, List<Student>> map = stream.collect(Collectors.groupingBy(student -> {
////            return student.getAge();
//            return student.getName();
//        }));
//        //无序
//        //k   对应 map 的key
//        //v   对应 map 的value对象
//        map.forEach((k, v) -> {
//            System.out.println(k + "-" + v);
//        });

        Map<String, List<Student>> map2 = stream.collect(Collectors.groupingBy(s -> {
            if (s.getAge() <= 80) {
                return "小";
            } else
                return "大";
        }));
        map2.forEach((k, v) -> System.out.println(k + "= " + v));
    }
}
